// 平台类型定义
export type Platform = 'h5' | 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao' | 'mp-qq' | 'mp-kuaishou' | 'mp-jd' | 'mp-lark' | 'mp-xhs' | 'app' | 'app-harmony' | 'quickapp-webview' | 'unknown'

// 获取系统信息
const systemInfo = uni.getSystemInfoSync()

// 平台检测
export const isH5 = systemInfo.uniPlatform === 'web'
export const isMp = systemInfo.uniPlatform?.startsWith('mp-') || false
export const isApp = systemInfo.uniPlatform === 'app'
export const isAndroid = systemInfo.platform === 'android'
export const isIOS = systemInfo.platform === 'ios'

// 具体平台检测
export const isMpWeixin = systemInfo.uniPlatform === 'mp-weixin'
export const isMpAlipay = systemInfo.uniPlatform === 'mp-alipay'
export const isMpBaidu = systemInfo.uniPlatform === 'mp-baidu'
export const isMpToutiao = systemInfo.uniPlatform === 'mp-toutiao'
export const isMpQQ = systemInfo.uniPlatform === 'mp-qq'
export const isMpKuaishou = systemInfo.uniPlatform === 'mp-kuaishou'
export const isMpJD = systemInfo.uniPlatform === 'mp-jd'
export const isMpLark = systemInfo.uniPlatform === 'mp-lark'
export const isMpXHS = systemInfo.uniPlatform === 'mp-xhs'
export const isAppHarmony = systemInfo.uniPlatform === 'app-harmony'
export const isQuickApp = systemInfo.uniPlatform === 'quickapp-webview'

// 获取当前平台
export const platform: Platform = (systemInfo.uniPlatform as Platform) || 'unknown'

// 获取平台中文名称
export function getPlatformName(): string {
  const platformNames: Record<Platform, string> = {
    'h5': 'H5',
    'mp-weixin': '微信小程序',
    'mp-alipay': '支付宝小程序',
    'mp-baidu': '百度小程序',
    'mp-toutiao': '抖音小程序',
    'mp-qq': 'QQ小程序',
    'mp-kuaishou': '快手小程序',
    'mp-jd': '京东小程序',
    'mp-lark': '飞书小程序',
    'mp-xhs': '小红书小程序',
    'app': 'App',
    'app-harmony': '鸿蒙App',
    'quickapp-webview': '快应用',
    'unknown': '未知平台',
  }

  return platformNames[platform] || '未知平台'
}

// 获取平台英文简称
export function getPlatformCode(): string {
  const platformCodes: Record<Platform, string> = {
    'h5': 'H5',
    'mp-weixin': 'WECHAT',
    'mp-alipay': 'ALIPAY',
    'mp-baidu': 'BAIDU',
    'mp-toutiao': 'TOUTIAO',
    'mp-qq': 'QQ',
    'mp-kuaishou': 'KUAISHOU',
    'mp-jd': 'JD',
    'mp-lark': 'LARK',
    'mp-xhs': 'XHS',
    'app': 'APP',
    'app-harmony': 'HARMONY',
    'quickapp-webview': 'QUICKAPP',
    'unknown': 'UNKNOWN',
  }

  return platformCodes[platform] || 'UNKNOWN'
}

// 检查是否支持某个功能
export function isSupportFeature(feature: string): boolean {
  const featureMap: Record<string, Platform[]> = {
    // 支付功能
    payment: ['h5', 'mp-weixin', 'mp-alipay', 'app'],
    // 分享功能
    share: ['mp-weixin', 'mp-qq', 'mp-toutiao', 'app'],
    // 定位功能
    location: ['h5', 'mp-weixin', 'mp-alipay', 'mp-baidu', 'app'],
    // 摄像头功能
    camera: ['h5', 'mp-weixin', 'mp-alipay', 'mp-baidu', 'app'],
    // 录音功能
    record: ['mp-weixin', 'mp-alipay', 'mp-baidu', 'app'],
    // 文件下载
    download: ['h5', 'mp-weixin', 'app'],
    // 打开外部应用
    openApp: ['app'],
    // 扫码功能
    scanCode: ['mp-weixin', 'mp-alipay', 'mp-baidu', 'app'],
    // 蓝牙功能
    bluetooth: ['mp-weixin', 'app'],
    // NFC 功能
    nfc: ['app'],
    // 指纹识别
    fingerprint: ['app'],
    // 面容识别
    faceID: ['app'],
    // 推送通知
    push: ['app'],
    // 悬浮窗
    floatingWindow: ['app'],
  }

  const supportedPlatforms = featureMap[feature] || []
  return supportedPlatforms.includes(platform)
}

// 获取设备信息
export function getDeviceInfo() {
  return {
    platform: getPlatformName(),
    platformCode: getPlatformCode(),
    brand: systemInfo.brand || '未知',
    model: systemInfo.model || '未知',
    system: systemInfo.system || '未知',
    version: systemInfo.version || '未知',
    SDKVersion: systemInfo.SDKVersion || '未知',
    screenWidth: systemInfo.screenWidth || 0,
    screenHeight: systemInfo.screenHeight || 0,
    windowWidth: systemInfo.windowWidth || 0,
    windowHeight: systemInfo.windowHeight || 0,
    pixelRatio: systemInfo.pixelRatio || 1,
    statusBarHeight: systemInfo.statusBarHeight || 0,
    safeArea: systemInfo.safeArea || null,
    safeAreaInsets: systemInfo.safeAreaInsets || null,
    navigationBarHeight: systemInfo.navigationBarHeight || 0,
    theme: systemInfo.theme || 'light',
  }
}

// 判断是否为开发环境
export function isDevelopment(): boolean {
  try {
    // 使用 import.meta.env 或 globalThis 来访问环境变量，避免直接使用 process
    return (import.meta.env?.NODE_ENV || (globalThis as any).__VITE_NODE_ENV__) === 'development'
  }
  catch {
    return false
  }
}

// 判断是否为生产环境
export function isProduction(): boolean {
  try {
    // 使用 import.meta.env 或 globalThis 来访问环境变量，避免直接使用 process
    return (import.meta.env?.NODE_ENV || (globalThis as any).__VITE_NODE_ENV__) === 'production'
  }
  catch {
    return true
  }
}

// 获取网络类型
export function getNetworkType(): Promise<string> {
  return new Promise((resolve) => {
    uni.getNetworkType({
      success: (res) => {
        resolve(res.networkType)
      },
      fail: () => {
        resolve('unknown')
      },
    })
  })
}

// 检查网络连接状态
export function checkNetworkStatus(): Promise<boolean> {
  return new Promise((resolve) => {
    uni.getNetworkType({
      success: (res) => {
        resolve(res.networkType !== 'none')
      },
      fail: () => {
        resolve(false)
      },
    })
  })
}

// 监听网络状态变化
export function onNetworkStatusChange(callback: (isConnected: boolean, networkType: string) => void) {
  uni.onNetworkStatusChange((res) => {
    callback(res.isConnected, res.networkType)
  })
}

// 判断是否支持暗黑模式
export function isSupportDarkMode(): boolean {
  return isSupportFeature('darkMode') || isH5 || isApp
}

// 获取当前主题模式
export function getCurrentTheme(): 'light' | 'dark' | 'auto' {
  try {
    return (systemInfo.theme as 'light' | 'dark') || 'light'
  }
  catch {
    return 'light'
  }
}

// 平台特定的存储前缀
export function getStoragePrefix(): string {
  return `${getPlatformCode()}_`
}

// 平台信息摘要
export const platformSummary = {
  name: getPlatformName(),
  code: getPlatformCode(),
  platform,
  isH5,
  isMp,
  isApp,
  isAndroid,
  isIOS,
  isDevelopment: isDevelopment(),
  isProduction: isProduction(),
  systemInfo,
}
